#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
from config import etc
from utils import log
from utils.session_base import ApiGuest
from utils.session_mc import BaseHandler
from controls import data_feedback_mysql


class FeedbackAddHandler(BaseHandler):
    @ApiGuest
    def post(self):
        content = self.ss_params.get('content')
        contact = self.ss_params.get('contact')

        if not content:
            log.w('params fetching error')
            res = {'idx': self.ss_idx, 'ret': 1, 'msg': etc.err_500, 'res': {}}
            self.write(json.dumps(res))
            self.finish()
            return

        if data_feedback_mysql.add_feedback_item({
            'contact': contact,
            'content': content,
        }):
            res = {'idx': self.ss_idx, 'ret': 0, 'msg': 'ok', 'res': {}}
        else:
            res = {'idx': self.ss_idx, 'ret': 1, 'msg': 'fail', 'res': {}}
        self.write(json.dumps(res))
        self.finish()
        return